<?php

namespace App\Http\Controllers\Admin;

use App\Models\AttendanceSet;
use App\Models\Feedback;
use App\Models\FeedbackDetail;
use App\Models\FeedbackSetDetail;
use App\Models\RewardPunishSetDetail;
use App\Http\Requests;
use App\Http\Requests\CompanyUpdateRequest;
use App\Http\Requests\AttendanceSetUpdateRequest;
use App\Models\Company;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\RewardPunishSet;
use Illuminate\Support\Facades\Response;
class SetController extends BaseController
{
    //
    public function companyEdit(){
        $company = Company::where('id',$this->corpid)->first();
        $years = explode(',',$company->years_array);
        rsort($years);
        $now = date('Y') + 10;
        $yearlist = array();
        for($i = $company->from_year - 6; $i <= $now;$i++){
            if(!in_array($i,$years)){
                $yearlist[] = array(
                    'id'=>$i,
                    'value'=>$i.'年',
                );
            }
        }
        $yearlist = json_encode($yearlist);
        return view("admin.set.company_edit")->with('years_set',$yearlist)->with('company',$this->company);
    }

    public function companyUpdate(CompanyUpdateRequest $request){
        if($this->user->getRole()==9 ){
            $data=$request->FillData();
            $this->company->update($data);
            return  $this->formatJson('success','修改成功',$this->company);
        }else{
            return  $this->formatJson('error','抱歉，您没有相关操作权限');
        }
    }

    public function attendanceEdit(Request $request){

        $year_param = $request->input('year', 0);
        $company = Company::where('id',$this->corpid)->first();
        //dd($company);
        if(!is_positive_int($year_param)){
            $year_param = getCurrentYear();
        }
        if(!in_array($year_param,explode(',',$company->years_array))){
            $year_param = $company->from_year;
        }

        $years = explode(',',$company->years_array);
        rsort($years);
        $now = date('Y') + 10;
        $yearlist = array();
        foreach($years as $y){
            $yearlist[] = array(
                'id'=>$y,
                'value'=>$y.'年',
            );
        }
        $yearlist = json_encode($yearlist);
        $set = AttendanceSet::where('year_param',$year_param)->where('corpid',$this->corpid)->first();
        if(is_mobile()){
            $view = 'admin.set.attendance_edit';
        }else{
            $view = 'pc_admin.set.attendance_edit';
        }
        return view($view)
            ->with('year_param', $year_param)
            ->with('yearlist', $yearlist)
            ->with('set',$set)
            ->with('company',$this->company);
    }



    public function postAddYearSet(Request $request){
        $result = array(
            'status'        => 'fail',
            'msg'           => 'ok',
            'data'          => '',
            'redirectUrl'   => ''
        );
        $year_param = $request->input('year', 0);
        $company = Company::where('id',$this->corpid)->first();
        if(!is_positive_int($year_param) || $year_param < 2010 ){
            $result['msg'] = '年份无效';
            return Response::json($result);
        }

        $years = explode(',',$company->years_array);
        rsort($years);
        if(in_array($year_param,$years)){
            $result['msg'] = '年份无效,该年份已设置';
            return Response::json($result);
        }
        $default = array(
            '很好'=>'5',
            '较好'=>'4',
            '正常'=>'3',
            '较差'=>'2',
            '很差'=>'1',
        );
        $names = Feedback::where('corpid',$this->corpid)->where('year_param',$years[0])->lists('name')->toArray();
        //$names = array('主动性','学习能力','合作精神','职业操守','专业技能');
        DB::beginTransaction();
        try{
            foreach ($names as $k=>$n){
                $feedback = new Feedback();
                $feedback->corpid = $this->corpid;
                $feedback->name = $n;
                $feedback->year_param = $year_param;
                $feedback->detail = serialize($default);
                $feedback->save();

                foreach ($default as $ky=>$v){
                    $set_detail = new FeedbackSetDetail();
                    $set_detail->set_id = $feedback->id;
                    $set_detail->set_name = $ky;
                    $set_detail->set_value = $v;
                    $set_detail->save();
                }
            }

            $default_jc = array(
                '高级'=>'5',
                '中级'=>'3',
                '低级'=>'1',
            );

            $default_jcp = array(
                '高级'=>'-5',
                '中级'=>'-3',
                '低级'=>'-1',
            );

            $reward = new RewardPunishSet();
            $reward->kind = 1;
            $reward->year_param = $year_param;
            $reward->corpid = $this->corpid;

            $lastrp = RewardPunishSet::where('corpid',$this->corpid)->where('year_param',$years[0])->where('kind','1')->first();
            if(empty($lastrp)){
                $reward->detail = serialize($default_jc);
                $reward->save();
                foreach ($default_jc as $ky=>$vy){
                    $set = new RewardPunishSetDetail();
                    $set->set_id = $reward->id;
                    $set->set_name = $ky;
                    $set->set_value = $vy;
                    $set->save();
                }
            }else{
                $djc = $lastrp->detail;
                $reward->detail = $djc;
                $reward->save();
                $djc = unserialize($djc);
                foreach ($djc as $ky=>$vy){
                    $set = new RewardPunishSetDetail();
                    $set->set_id = $reward->id;
                    $set->set_name = $ky;
                    $set->set_value = $vy;
                    $set->save();
                }
            }



            $punish = new RewardPunishSet();
            $punish->kind = 2;
            $punish->year_param = $year_param;
            $punish->corpid = $this->corpid;

            $lastp = RewardPunishSet::where('corpid',$this->corpid)->where('year_param',$years[0])->where('kind','2')->first();
            if(empty($lastp)){
                $punish->detail = serialize($default_jcp);
                $punish->save();
                foreach ($default_jcp as $ky=>$vy){
                    $set = new RewardPunishSetDetail();
                    $set->set_id = $punish->id;
                    $set->set_name = $ky;
                    $set->set_value = $vy;
                    $set->save();
                }
            }else{
                $djcp = $lastp->detail;
                $punish->detail = $djcp;
                $punish->save();
                $djcp = unserialize($djcp);
                foreach ($djcp as $ky=>$vy){
                    $set = new RewardPunishSetDetail();
                    $set->set_id = $punish->id;
                    $set->set_name = $ky;
                    $set->set_value = $vy;
                    $set->save();
                }
            }



            $att_set = new AttendanceSet();
            $att_set->corpid = $this->corpid;
            $att_set->year_param = $year_param;
            $att_set->save();

            $years[] = $year_param;
            $company->years_array = implode(',',$years);
            $company->save();

        }catch (\PDOException $e) {
            DB::rollback();
            Log::info($e->getMessage());
            $result['msg'] = '数据保存失败';
            return Response::json($result);
        }
        DB::commit();
        $result['status'] = 'success';
        $result['msg'] = '成功!';
        return Response::json($result);
    }
    
    
    
    public function attendanceUpdate(AttendanceSetUpdateRequest $request){
        $data=$request->FillData();
        $year_param = $data['year'];
        $AttendanceSet=AttendanceSet::where('year_param',$year_param)->where('corpid',$this->corpid)->first();
        if(empty($AttendanceSet)){
            $AttendanceSet = new AttendanceSet();
            $AttendanceSet->year_param = $year_param;
            $AttendanceSet->corpid = $this->corpid;
            $AttendanceSet->save();
        }
        $AttendanceSet->update($data);
        return  $this->formatJson('success','修改成功',$AttendanceSet,'/admin/set/company_edit');
    }
}
